/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.view;

import java.awt.Component;
import java.util.Stack;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import offset.nodes.client.virtual.model.SchemaProperty;

/**
 *
 * @author Walter Lütgenau
 */
public class MappedSchemaNodeRenderer extends SchemaNodeRenderer {
    String[] pathes;
    static ImageIcon mappedNodeIcon = new ImageIcon(MappedSchemaNodeRenderer.class.getResource("/offset/nodes/client/view/resources/sc_mapped.png"));
    static ImageIcon mappedPropertyIcon = new ImageIcon(MappedSchemaNodeRenderer.class.getResource("/offset/nodes/client/view/resources/sc_mapped_property.png"));
    
    /** Creates a new instance of MappedSchemaNodeRenderer */
    public MappedSchemaNodeRenderer(String[] pathes) {
        this.pathes = pathes;
    }
    
    public Component getTreeCellRendererComponent(JTree tree, Object value,
        boolean sel,
        boolean expanded,
        boolean leaf, int row,
        boolean hasFocus) {
        Component ret = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
        if ( isMapped(treeNode) ) {
            if ( treeNode.getUserObject() instanceof SchemaProperty)
                setIcon(mappedPropertyIcon);
            else
                setIcon(mappedNodeIcon);
        }
        else {
            if ( treeNode.getUserObject() instanceof SchemaProperty)
                setIcon(propertyIcon);
            else
                setIcon(nodeIcon);
        }
        
        return ret;
    }
    
    protected String getPath(DefaultMutableTreeNode node) {
        Stack reverse = new Stack();
        while( node != null) {
            reverse.push(node);
            node = (DefaultMutableTreeNode) node.getParent();
        }
        
        StringBuffer buf = new StringBuffer();
        while( reverse.size() > 0) {
            buf.append(reverse.pop().toString());
            if ( reverse.size() > 0)
                buf.append("/");
        }
        
        return buf.toString();
    }
    
    protected boolean isMapped(DefaultMutableTreeNode node) {
        boolean result = false;
        String path = getPath(node);
        
        for( int i=0; i<pathes.length; i++)
            if ( pathes[i] != null && pathes[i].equals(path))
                return true;
        
        return result;
    }
}
